﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SuperFox.SuperServiceReference;
using System.Windows.Media.Imaging;
using System.Windows.Data;

namespace SuperFox
{
    public partial class clientShoppingCart : UserControl
    {
        public clientShoppingCart()
        {
            InitializeComponent();

            // link this to the user 

            //(App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart[0].Options.Values[0].pov_pricechange

            this.DataContext = (App.Current.RootVisual as MainPage).CurrentUser;
            //(this.DataContext as DbUsers).ShoppingCart.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ShoppingCart_CollectionChanged);
            //(this.DataContext as DbUsers).ShoppingCart[0].Options.Values.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Values_CollectionChanged);
            //(this.DataContext as DbUsers).ShoppingCart.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ShoppingCart_PropertyChanged);
            //(this.DataContext as DbUsers).PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(clientShoppingCart_PropertyChanged);
            //(this.DataContext as DbUsers).ShoppingCart.ChildPropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ShoppingCart_ChildPropertyChanged);
            //(this.DataContext as DbUsers).ShoppingCart[0].Options.Values[0].PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(clientShoppingCart_PropertyChanged);
            
        }


        //void ShoppingCart_ChildPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{

        //}


        //void clientShoppingCart_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{
            
        //}





        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            clientWelcomePage wp = new clientWelcomePage();

            wp.Products = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Products;
            wp.ClientNameTb.Text = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_name;

            (App.Current.RootVisual as MainPage).LoadContent(wp);

        }

        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            // proceed to next step

            (App.Current.RootVisual as MainPage).LoadContent(new clientOrderForm());

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // remove button handler

            if (System.Windows.Browser.HtmlPage.Window.Confirm("Do you want to remove this item from the order?"))
            {
                (this.DataContext as DbUsers).ShoppingCart.Remove((sender as Button).DataContext as DbProducts);
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            //ItemsGrid.ItemsSource = (this.DataContext as DbUsers).ShoppingCart;
        }

        //private void TestButton_Click(object sender, RoutedEventArgs e)
        //{
        //   // ((sender as Button).DataContext as DbProducts).Options
        //}

        private void TextBlock_Loaded(object sender, RoutedEventArgs e)
        {
            Binding b = new Binding();

            b.Source = (sender as TextBlock).DataContext;
            b.Converter = new TotalPriceConverter();
            b.Path = new PropertyPath("TotalSelected");
            b.ConverterParameter = ((sender as TextBlock).DataContext as DbProductOptionsValues).Price;


            (sender as TextBlock).SetBinding(TextBlock.TextProperty, b);
        }

        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            if (System.Windows.Browser.HtmlPage.Window.Confirm("Do you want to clear your shopping cart?"))
            {
                (this.DataContext as DbUsers).ShoppingCart.Clear();
            }
        }



        //public void CalculateTotal()
        //{

        //}

        private void TotlaTb_Loaded(object sender, RoutedEventArgs e)
        {
            // hook up bining to calculate total

            Binding b = new Binding()
            {
                Source = (this.DataContext as DbUsers),
                Path = new PropertyPath("ShoppingCart"),
                Mode = BindingMode.OneWay,
                Converter = new SumTotalConverter()
            };

            (sender as TextBlock).SetBinding(TextBlock.TextProperty, b);
        }

        private void TotlaGSTTb_Loaded(object sender, RoutedEventArgs e)
        {
            Binding b = new Binding()
            {
                Source = (this.DataContext as DbUsers),
                Path = new PropertyPath("ShoppingCart"),
                Mode = BindingMode.OneWay,
                Converter = new SumTotalGSTConverter()
            };

            (sender as TextBlock).SetBinding(TextBlock.TextProperty, b);
        }

        private void GSTTb_Loaded(object sender, RoutedEventArgs e)
        {
            Binding b = new Binding()
            {
                Source = (this.DataContext as DbUsers),
                Path = new PropertyPath("ShoppingCart"),
                Mode = BindingMode.OneWay,
                Converter = new GSTConverter()
            };

            (sender as TextBlock).SetBinding(TextBlock.TextProperty, b);
        }

        private void Freight_Loaded(object sender, RoutedEventArgs e)
        {
            Binding b = new Binding()
            {
                Source = (this.DataContext as DbUsers),
                Path = new PropertyPath("ShoppingCart"),
                Mode = BindingMode.OneWay,
                Converter = new FreightConverter()
            };

            (sender as TextBlock).SetBinding(TextBlock.TextProperty, b);
        }

        //private void TextBlock_Loaded(object sender, RoutedEventArgs e)
        //{
        //    DbProductOptionsValues p = (sender as TextBlock).DataContext as DbProductOptionsValues;
            
        //}

        //private void TextBlock_Loaded(object sender, RoutedEventArgs e)
        //{
        //    // create the binding for the sub total

        //    DbProducts p = (sender as TextBlock).DataContext as DbProducts;

        //    Binding b = new Binding()
        //    {
        //        Converter = new TotalPriceConverter(),
        //        Mode = BindingMode.OneWay,
        //        Source = p,
        //        Path = new PropertyPath(
        //    };
        //}


    }
}
